home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 May / EnigmA AMIGA RUN 18 (1997)(G.R. Edizioni)(IT)[!][issue 1997-05][EAR-CD II].iso / softwareupdate / system / amigados / advancedroutines / example2.c < prev    next >
C/C++ Source or Header  |  1996-10-10  |  7KB  |  227 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE)           Amiga C Club (ACC) */
  4. /* --------------------------           ------------------ */
  5. /*                                                         */
  6. /* Manual:  AmigaDOS                    Amiga C Club       */
  7. /* Chapter: Advanced Routines           Tulevagen 22       */
  8. /* File:    Example2.c                  181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    93-03-17                                       */
  11. /* Version: 1.0                                            */
  12. /*                                                         */
  13. /*   Copyright 1993, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20.  
  21. /* This example does exactly the same thing as the previous one, */
  22. /* it simply demonstrates how to use the Examine() function.     */
  23. /* However, this example uses the new AllocDosObject() and       */
  24. /* FreeDosObject() functions which were introduced in Release 2. */
  25. /* You should use these new functions (if possible) instead of   */
  26. /* using the older method of allocating a fixed amount of memory */
  27. /* for the dos object (the FileInfoBlock structure).             */ 
  28. /*                                                               */
  29. /* This example can only be used with dos library V37 or higher. */
  30.  
  31.  
  32.  
  33. /* Include the dos library definitions: */
  34. #include <dos/dos.h>
  35.  
  36. /* Now we include the necessary function prototype files:         */
  37. #include <clib/dos_protos.h>       /* General dos functions...    */
  38. #include <stdio.h>                 /* Std functions [printf()...] */
  39. #include <stdlib.h>                /* Std functions [exit()...]   */
  40. #include <string.h>                /* Std functions [strlen()...] */
  41.  
  42.  
  43.  
  44. /* Set name and version number: */
  45. UBYTE *version = "$VER: AmigaDOS/Advanced Routines/Example2 1.0";
  46.  
  47.  
  48.  
  49. /* Declare an external global library pointer to the Dos library: */
  50. extern struct DosLibrary *DOSBase;
  51.  
  52.  
  53.  
  54. /* Declared our own function(s): */
  55.  
  56. /* Our main function: */
  57. int main( int argc, char *argv[] );
  58.  
  59.  
  60.  
  61. /* Main function: */
  62.  
  63. int main( int argc, char *argv[] )
  64. {
  65.   /* "BCPL" pointer to our lock: */
  66.   BPTR my_lock;
  67.  
  68.   /* Pointer to our FileInfoBlock which we will allocate: */
  69.   struct FileInfoBlock *my_fib;
  70.  
  71.   /* AmigaDOS boolean check variable: */
  72.   LONG ok;
  73.  
  74.  
  75.  
  76.   /* We need dos library V37 or higher: */
  77.   if( DOSBase->dl_lib.lib_Version < 37 )
  78.   {
  79.     /* The user has a dos library which is too old! */
  80.     printf( "This program needs dos library V37 or higher!\n" );
  81.  
  82.     /* Exit with an error code: */
  83.     exit( 20 );
  84.   }
  85.  
  86.  
  87.  
  88.   /* This program needs one arguement:  */
  89.   /* (a file, directory or volume name) */
  90.   if( argc != 2 )
  91.   {
  92.     /* Wrong number of arguments! */
  93.     printf( "Error! Wrong number of arguments!\n" );
  94.     printf( "You must enter a file, directory or volume name.\n" );
  95.     printf( "Example2 Name/A\n" ); /* Simple template */
  96.  
  97.     /* Exit with an error code: */
  98.     exit( 21 );
  99.   }
  100.  
  101.  
  102.  
  103.   /* 1. Try to lock the object: (Shared access is enough.) */
  104.   my_lock = Lock( argv[ 1 ], SHARED_LOCK );
  105.   
  106.   /* Could we lock the object? */
  107.   if( !my_lock )
  108.   {
  109.     /* Problems! Inform the user: */
  110.     printf( "Could not lock the object!\n" );
  111.  
  112.     /* Exit with an error code: */
  113.     exit( 22 );
  114.   }
  115.  
  116.  
  117.  
  118.   /* 2. Create a FileInfoBlock structure with help of the     */
  119.   /* new AllocDosObject() function. Note that this function   */
  120.   /* needs dos library V37 or higher! Note also that anything */
  121.   /* we allocate with this function must be deallocated with  */
  122.   /* help of the FreeDosObject() function!                    */
  123.   /* (Type FileInfoBlock structure and no Tags.)              */
  124.   my_fib = AllocDosObject( DOS_FIB, NULL );
  125.  
  126.   /* Check if we have allocated the memory successfully: */
  127.   if( !my_fib )
  128.   {
  129.     /* Problems! Inform the user: */
  130.     printf( "Could not allocate the FileInfoBlock!\n" );
  131.  
  132.     /* Unlock the object: */
  133.     UnLock( my_lock );
  134.  
  135.     /* Exit with an error code: */
  136.     exit( 23 );
  137.   };
  138.  
  139.  
  140.  
  141.   /* 3. Get some information about the object we have locked: */
  142.   ok = Examine( my_lock, my_fib );
  143.  
  144.   /* Any problems? */
  145.   if( !ok )
  146.   {
  147.     /* Problems! Inform the user: */
  148.     printf( "Could not examine the object!\n" );
  149.  
  150.     /* Deallocate the FileInfoBlock structure wich we have */
  151.     /* created with help of the AllocDosObject() function: */
  152.     FreeDosObject( DOS_FIB, my_fib );
  153.  
  154.     /* Unlock the object: */
  155.     UnLock( my_lock );
  156.  
  157.     /* Exit with an error code: */
  158.     exit( 24 );
  159.   }
  160.  
  161.  
  162.  
  163.   /* 4. You may now examine the FileInfoBlock structure! */
  164.   printf( "Name:      %s\n", my_fib->fib_FileName );
  165.  
  166.   if( my_fib->fib_DirEntryType < 0 )
  167.     printf( "Type:      File\n" );
  168.   else
  169.     printf( "Type:      Directory or Volume\n" );
  170.  
  171.   printf( "Size:      %d\n", my_fib->fib_Size );
  172.   printf( "Blocks:    %d\n", my_fib->fib_NumBlocks );
  173.   printf( "Comment:   %s\n",
  174.     my_fib->fib_Comment[0] ? my_fib->fib_Comment : "No comment" );
  175.  
  176.   printf( "Protection bits:\n" );
  177.   printf( "  Delete:  %s\n",
  178.     my_fib->fib_Protection & FIBF_DELETE ? "On" : "Off" );
  179.  
  180.   printf( "  Execute: %s\n",
  181.     my_fib->fib_Protection & FIBF_EXECUTE ? "On" : "Off" );
  182.  
  183.   printf( "  Write:   %s\n",
  184.     my_fib->fib_Protection & FIBF_WRITE ? "On" : "Off" );
  185.  
  186.   printf( "  Read:    %s\n",
  187.     my_fib->fib_Protection & FIBF_READ ? "On" : "Off" );
  188.  
  189.   printf( "  Archive: %s\n",
  190.     my_fib->fib_Protection & FIBF_ARCHIVE ? "On" : "Off" );
  191.  
  192.   printf( "  Pure:    %s\n",
  193.     my_fib->fib_Protection & FIBF_PURE ? "On" : "Off" );
  194.  
  195.   printf( "  Script:  %s\n",
  196.     my_fib->fib_Protection & FIBF_SCRIPT ? "On" : "Off" );
  197.  
  198.   printf( "Last changed: (Internal datestamp value)\n" );
  199.   printf( "  Days:    %d\n", my_fib->fib_Date.ds_Days );
  200.   printf( "  Minutes: %d\n", my_fib->fib_Date.ds_Minute );
  201.   printf( "  Ticks:   %d\n", my_fib->fib_Date.ds_Tick );
  202.  
  203.  
  204.  
  205.   /* 5. Deallocate the FileInfoBlock structure wich we have */
  206.   /* created with help of the AllocDosObject() function:    */
  207.   /* (Remember that we must use this function if you        */
  208.   /* allocated the object with help of the AllocDosObject() */
  209.   /* function. The advantage with AllocDosObject() is that  */
  210.   /* the size of the object can vary betweeen different     */
  211.   /* releases of the dos library and your program will      */
  212.   /* still be able to run. Since the size may vary you      */
  213.   /* must of course use some function which can deallocate  */
  214.   /* all memory that was allocated, and not just use a      */
  215.   /* fixed size as we have to do when we use AllocMem() and */
  216.   /* FreeMem().)                                            */
  217.   FreeDosObject( DOS_FIB, my_fib );
  218.  
  219.   /* 6. Unlock the file: */
  220.   UnLock( my_lock );  
  221.  
  222.   /* The End! */
  223.   exit( 0 );
  224. }
  225.  
  226.  
  227.